#!/bin/bash

# 查看当前服务器CPU全部核心数量
# CPU全部核心数量，测试全核性能需要配置，默认为全部cpu
cpus=$(grep -c processor /proc/cpuinfo)


base_dir=$(pwd)
# 测试轮次，默认1次，按需修改
source $base_dir/user_config


echo "[INFO] 安装编译和运行依赖的组件：gcc make libaio libaio-devel perl zlib-devel gcc-gfortran expect tar"
yum install -y gcc make libaio libaio-devel perl zlib-devel gcc-gfortran expect tar  2>&1 > /dev/null
if [ $? -eq 0 ]; then
    echo "[INFO] yum-安装成功"
else
    echo "[ERROR] yum-安装失败，退出测试 Unixbench"
    exit 1
fi

echo "[INFO] 关闭交换区分"
swapoff -a
echo "[INFO] 关闭防火墙 firewalld"
systemctl stop firewalld
systemctl disable firewalld


## 源码编译安装 unixbench
if [ -d "$base_dir/byte-unixbench-5.1.3" ]; then
    echo "[INFO] 删除 unixbench 源码目录，使用重新解压后的源码"
    rm -rf "$base_dir/byte-unixbench-5.1.3"
fi

echo "[INFO] 解压缩 Unixbench 源码包"
tar -zxf $base_dir/byte-unixbench-5.1.3.tar.gz
cd $base_dir/byte-unixbench-5.1.3/UnixBench/

echo "[INFO] 编译 Unixbench"
sed -i "s/'system'    => { 'name' => \"System Benchmarks\", 'maxCopies' => 16 },/'system'    => { 'name' => \"System Benchmarks\", 'maxCopies' => 1024 },/" Run
sed -i "s/'misc'      => { 'name' => \"Non-Index Benchmarks\", 'maxCopies' => 16 },/'misc'      => { 'name' => \"Non-Index Benchmarks\", 'maxCopies' => 1024 },/" Run
make clean && make all  >/dev/null



## 开始测试
sync
echo 3 > /proc/sys/vm/drop_caches
sleep 3s

if [ -z "${log_file}" ]; then
    cur_time=`date +"%Y%m%d_%H%M%S"`
    log_dir="/home/log_${cur_time}_`uname -r`"
    mkdir -p "${log_dir}"
    declare log_file="${log_dir}"/unixbench
fi

echo "[INFO] 开始测试 Unixbench"
for i in $(seq $unixbench_run_counts);
do
    echo "[INFO] -------- 第 $i 次 Unixbench 性能测试 -----------"
    $base_dir/byte-unixbench-5.1.3/UnixBench/Run -c 1 -c $cpus | tee -a "${log_file}_${i}"
    if [ $? -ne 0 ]; then
        echo "./Run -c 1 -c $cpus 运行错误，请检查！"
        exit 1
    fi
    echo -e "\033[32m [INFO] `date` unixbench 运行完成，结果存放在 ${log_file}_${i} \033[0m"
done

